package com.kawa.kemanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kawa.kemanage.entity.NewsCategory;
import com.kawa.kemanage.entity.NewsDetail;
import com.kawa.kemanage.mapper.NewsMapper;
import com.kawa.kemanage.service.NewsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: KAIyo
 * @Date: 2025/6/3 0:28
 * @Version: v1.0.0
 * @Description: TODO
 **/
@Service
@Transactional
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    /**
     * 根据类型，文章标题，发布时间,查询所有新闻
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PageInfo<NewsDetail> findAllNews(Integer pageNum, Integer pageSize, Integer categoryId, String title, Date beginDate, Date endDate) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<NewsDetail> pageInfo = new PageInfo<>(newsMapper.findAllNews(categoryId, title, beginDate, endDate));
        return pageInfo;
    }



    /**
     * 根据id查询新闻
     * @param id
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public NewsDetail findNewsDetail(Integer id) {
        NewsDetail newsDetail = newsMapper.findNewsDetailById(id);
        if (newsDetail == null) {
            return null;
        }
        return newsDetail;
    }
    /**
     * 新增新闻
     * @param news
     * @return
     */
    @Override
    public boolean addNewsDetail(NewsDetail news) {
        return  newsMapper.addNewsDetail(news)?true:false;
    }
    /**
     * 根据id修改新闻
     * @param news
     * @return
     */
    @Override
    public boolean updateNewsDetailById(NewsDetail news) {
        return newsMapper.updateNewsDetailById(news)?true:false;
    }

    /**
     * 根据id删除新闻
     * @param id
     * @return
     *
     * */
    @Override
    public boolean deleteNewsDetailById(Integer id) {
        return newsMapper.deleteNewsDetailById(id)?true:false;
    }




    /**
     * 查询新闻类型
     * @param
     * @return
     *
     * */
    @Override
    public List<NewsCategory> findNewsCategory() {
        return newsMapper.findNewsCategory();
    }

}
